home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dsiic2.zip / L_WIN1.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  12KB  |  414 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_WIN1.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <dos.h>
  7.  
  8. #if defined QUICKC
  9.  
  10. #include "malloc.h"
  11. #include "memory.h"
  12.  
  13. #endif
  14.  
  15. #if defined TURBOC
  16.  
  17. #include <alloc.h>
  18. #include <mem.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>   /* [* */
  22. #endif
  23.  
  24.  
  25. /*****************************************************************
  26.  
  27.  Usage: int win_make(int x, int y, int width,int height \
  28.             char *array char *title, char frame_attr,char win_attr);
  29.  
  30.   int x,y =  column,row of upper left corner of active window area
  31.              (not upper left frame corner);
  32.   int width= width of window (interior).
  33.   int height= height of window (interior).
  34.  
  35.   char *array= pointer to string containing frame elements
  36.                (macros defined in mydef.h).
  37.   char *title= title to appear in upper left corner
  38.                of frame.
  39.   char frame_attr=  attribute to use when drawing frame.
  40.   char win_attr= default attribute for window.
  41.  
  42.   Creates a window, returns a "handle" (integer) which is used
  43.   to access the window in later operations.  The newly created
  44.   window is placed on top of all existing windows, and is the
  45.   default active window.  The newly created window is cleared.
  46.   A -1 is returned if too many window are opened.
  47.  
  48.   The window created will be resized and repositioned if necessary
  49.   to fit the screen.
  50.  
  51. *****************************************************************/
  52.  
  53. int win_make(int x,int y,int width,int height,char *array,
  54.              char *title, char frame_attr, char win_attr)
  55. {
  56. extern struct screen_structure scr;
  57. extern struct window_structure w[];
  58.  
  59.  int t,b,l,r;  /* hold calculated values of top,bottom,left,right */
  60.  
  61.    /* check parameters and correct if necessary */
  62.  
  63.    if(scr.ptr==MAX_WINDOW)return(-1);
  64.  
  65.    /* adjust x,y,width & height to fit screen */
  66.  
  67.   if(array[0]=='\0'){                             /* no frame */
  68.  
  69.     if(x<1)x=1;                                   /* too far left? */
  70.     if(y<1)y=1;                                   /* too far up? */
  71.     if(width>scr.columns) width=scr.columns;      /* too wide? */
  72.     if(height>scr.rows) height=scr.rows;          /* too high? */
  73.     if(x+width>scr.columns) x=scr.columns-width+1;/* too far right?*/
  74.     if(y+height>scr.rows) y=scr.rows-height+1;    /* too far down?*/
  75.  
  76.   }
  77.   else{                                        /* frame present */
  78.    if(x<2)x=2;                                 /* allow for frame */
  79.    if(y<2)y=2;
  80.    if(width+2>scr.columns) width=scr.columns-2;   /* too wide? */
  81.    if(height+2>scr.rows) height=scr.rows-2;       /* too high? */
  82.    if(x+width+2>scr.columns) x=scr.columns-width; /* too far right */
  83.    if(y+height+1>scr.rows) y=scr.rows-height;     /* too far down */
  84.   }
  85.  
  86.   if (array[0]=='\0')                       /* no frame */
  87.     w[scr.list[scr.ptr+1]].frame=FALSE;
  88.   else                                      /* frame present */
  89.     w[scr.list[scr.ptr+1]].frame=TRUE;
  90.  
  91.       t=y;b=y+height-1;l=x;r=x+width-1; /* calc. window coordinates*/
  92.  
  93.      win_save();    /* save window (include frame) */
  94.      scr.ptr++;     /* increment screen pointer */
  95.  
  96.      /* make new window the active window */
  97.      scr.active=scr.list[scr.ptr]; 
  98.  
  99.  /* release window so we can write anywhere
  100.     (the printing routines can't write outside existing window
  101.     so we temporarily expand window so we can draw frame)      */
  102.  
  103.   scr.top=1;
  104.   scr.bottom=scr.rows;
  105.   scr.right=scr.columns;
  106.   scr.left=1;
  107.  
  108.   if (array[0]!='\0'){   /* if frame requested */
  109.  
  110.    draw_frame(x-1,y-1,width,height,array,title,frame_attr);
  111.  
  112.   }
  113.  
  114. /* set current attribute to window attribute */
  115. scr.current = win_attr; 
  116.  
  117. w[scr.active].attribute=win_attr;  /* save the window attribute */
  118.  
  119. /* set active window values  */
  120.  
  121. w[scr.active].top=t;w[scr.active].bottom=b; /* save top and bottom */
  122. w[scr.active].left=l;w[scr.active].right=r; /* save left and right */
  123.  
  124. update_margins();
  125.  
  126.     /* set default "underscore" cursor for new window */
  127.  
  128.     if (scr.mode == COLOR_80 || scr.mode == BW_80){
  129.             w[scr.active].start=6; w[scr.active].end=7;
  130.     }
  131.  
  132.     if (scr.mode == MONOCHROME ){
  133.             w[scr.active].start=11;w[scr.active].end = 12;
  134.     }
  135.  
  136.     /* save cursor information */
  137.     cursor(NORMAL_CURSOR);  /* change acutal cursor */
  138.      cls();
  139.  
  140.  
  141. return(scr.active);  /* return the window "handle" */
  142.  
  143. }
  144.  
  145.  
  146. /*****************************************************************
  147.  
  148.  Usage: void win_save(void);
  149.  
  150.  Saves the screen buffer of the top window.
  151.  Used by other routines when the top window is about to be
  152.  over written or the screen is being redrawn.
  153.  
  154.  This function should not be called directly.
  155.  
  156. *****************************************************************/
  157.  
  158. void win_save(void)
  159.  
  160. {
  161. extern struct screen_structure scr;
  162. extern struct window_structure w[];
  163.  
  164.  int y;
  165.  char far *dest_ptr;
  166.  char far *scrn_ptr;
  167.  int buff_size;  /* memory size required to hold saved image */
  168.  int t,b,l,r;
  169.  
  170. /* set t,b,l,r to represent window top,bottom,left and right
  171.    adjust to include frame if necessary */
  172.  
  173.    if (w[scr.active].frame==TRUE){
  174.      t=scr.top-1;b=scr.bottom+1;l=scr.left-1;r=scr.right+1;
  175.     }
  176.     else{
  177.      t=scr.top;b=scr.bottom;l=scr.left;r=scr.right;
  178.     }
  179.    /* copy external screen pointer to local pointer */
  180.    scrn_ptr=scr.buffer; 
  181.  
  182.     if (scr.ptr > MAX_WINDOW){
  183.       cls();puts("too many windows ");exit(1);
  184.       return;
  185.     }
  186.  
  187.     /* calculate memory required to hold screen image */
  188.     buff_size=( (r-l+1)*(b-t+1) )*2;
  189.  
  190.     if (w[scr.active].buffer==NULL)  /* if no memory is allocated */
  191.     w[scr.active].buffer =
  192.       (char  *)malloc (buff_size*sizeof(unsigned char));
  193.  
  194.               /* set up destination pointer (dest_ptr) and
  195.                  screen pointer (scrn_ptr */
  196.  
  197.               dest_ptr=w[scr.active].buffer;
  198.  
  199.               /* initialize pointers */
  200.               scrn_ptr=scr.buffer;
  201.               scrn_ptr=scrn_ptr+((t-1)*(scr.columns*2))+(2*(l-1));
  202.               dest_ptr=w[scr.active].buffer;
  203.  
  204.     /* move each row from screen to buffer */
  205.     for(y=1 ;y<=b-t+1;y++){  
  206.       move_scr_mem(scrn_ptr,dest_ptr,2*(r-l+1));
  207.       scrn_ptr=scrn_ptr +(2*scr.columns); /* update screen pointer */
  208.       dest_ptr=dest_ptr+ (2*(r-l+1));/* update destination pointer */
  209.     }
  210.  
  211. }  /* end function */
  212.  
  213.  
  214. /*****************************************************************
  215.  
  216.  Usage: void win_delete(int handle);
  217.  
  218.  Deletes the window indicated.
  219.  Rearranges window list to reflect change.
  220.  
  221. *****************************************************************/
  222.  
  223. void win_delete(int handle)
  224. {
  225. extern struct screen_structure scr;
  226. extern struct window_structure w[];
  227.  
  228. int location,i;
  229.  
  230. if (handle==0) return;  /* can't delete initial window */
  231.  
  232. /*if it is the top window call win_delete_top */
  233. if (handle==scr.active) {  
  234.       win_delete_top();
  235.       return;
  236. }
  237.  
  238. location=win_validate(handle);
  239. if(location==-1) return;         /* not a window */
  240.  
  241. win_save();  /* save top window */
  242.  
  243. /* Deleting a window requires that the window list be
  244.    rearranged.   We shift all values after the window
  245.    being deleted forward one, then put the deleted
  246.    window at the end      */
  247.  
  248.  
  249. for(i=location;i<scr.ptr+1;i++) scr.list[i]=scr.list[i+1];
  250.  
  251. scr.list[scr.ptr]=handle;  /* move window to top of list */
  252.  
  253. if (w[handle].buffer!=NULL){
  254.  free(w[handle].buffer);  /*free buffer of deleted window */
  255.  w[handle].buffer=NULL;  /* NULL pointer */
  256. }
  257. scr.ptr--;   /* point down one level (we have one less window) */
  258. win_redraw_all();
  259. }
  260.  
  261.  
  262. /*****************************************************************
  263.  
  264.  Usage: int win_validate(int handle);
  265.  
  266.  Checks the list of allocated windows to see if the
  267.  Window handle indicated is valid.
  268.  
  269.  Returns a -1 if not valid, else returns the location
  270.  of the window in the list.
  271.  
  272. *****************************************************************/
  273.  
  274. int win_validate(int handle)
  275.  
  276. {
  277. extern struct  screen_structure scr;
  278. extern struct window_structure w[];
  279.  
  280. int i,location=-1;
  281.  for(i=0;i<=scr.ptr;i++){   /* find if handle is allocated window */
  282.   if (scr.list[i]==handle){
  283.    location=i;
  284.    break;
  285.   }
  286.  }
  287.  return location;
  288. }
  289.  
  290.  
  291. /*****************************************************************
  292.  
  293.  Usage: void win_delete_top(void);
  294.  
  295.  deletes the top window.
  296.  
  297. *****************************************************************/
  298.  
  299.  
  300. void win_delete_top(void)
  301. {
  302. extern struct  screen_structure scr;
  303.  
  304. if (scr.ptr==0)return;  /* can't delete initial window */
  305.  
  306.  
  307. scr.ptr--;              /* set the window pointer down one level */
  308. scr.active=scr.list[scr.ptr];
  309.  
  310. update_margins();
  311. win_redraw_all();      /* redraw all windows */
  312.  
  313. /* win_redraw will free buffer of top window when done
  314.    we do not need to do it here */
  315. }
  316.  
  317.  
  318. /*****************************************************************
  319.  
  320.  Usage: void draw_frame(int x,int y,int width,int height,char *array,
  321.                          char *title,char attribute)
  322.  
  323.  int x,y = Upper left corner of frame.
  324.  int width,height= Width and height of interior of frame.
  325.  char *array= Array of characters used to build frame. (see mydef.h)
  326.  char *title= Title to use in upper left corner of frame
  327.  int attribute= Text attribute to use for frame.
  328.  
  329. *****************************************************************/
  330.  
  331. void draw_frame (int x,int y,int width,int height,char *array,\
  332.                  char *title,char attribute)
  333. {
  334. extern struct screen_structure scr;
  335.  
  336.  int old_attribute;
  337.  int i,j,ctr,u_right,u_left;
  338.  int x2,y2;
  339.  char string[255];
  340.  
  341.  old_attribute=scr.current;  /* save current attribute */
  342.  scr.current=attribute;
  343.      /* draw first line of frame */
  344.      string[0]= array[0];                      /* left corner */
  345.      for(i=1;i<=width;i++) string[i]=array[4]; /* horizontal part */
  346.      string[i++]=array[1]; string[i]='\0';     /* right corner and
  347.                                                   terminator */
  348.       x2=x;y2=y;
  349.       dma_print(&x2,&y2,&string[0]);          /* print string */
  350.  
  351.      if (title!=NULL)          /*is there a title ? */
  352.        if (strlen (title)<= width) {  /* will it fit? */
  353.         x2=x+1;y2=y;
  354.         dma_print(&x2,&y2,title );
  355.        }
  356.  
  357.      y++;   /* move to next line */
  358.  
  359.        for (i=0; i<height;i++){         /* print vertical border */
  360.         string[0]=array[5];string[1]='\0';  /* left border */
  361.         x2=x;y2=y;
  362.         dma_print(&x2,&y2,string);
  363.         x2=x+width+1;y2=y++;         /* right border */
  364.         dma_print(&x2,&y2,string);
  365.       }
  366.           string[0]=array[2];        /* draw bottom line of frame */
  367.           for(i=1;i<=width;i++) string[i]=array[4];
  368.           string[i++]=array[3];string[i]='\0';
  369.           x2=x;y2=y++;
  370.           dma_print(&x2,&y2,&string[0]);
  371.  
  372.           scr.current=old_attribute;    /* restore old attribute */
  373. }
  374.  
  375.  
  376. /*****************************************************************
  377.  
  378.  Usage: int win_center(int width,int height,char *array
  379.                      char *title, char frame_attr,char win_attr);
  380.  
  381.  
  382.   int width= width of window (interior).
  383.   int height= height of window (interior).
  384.  
  385.   char *array= pointer to string containing frame elements
  386.                (macros defined in mydef.h).
  387.   char *title= title to appear in upper left corner
  388.                of frame.
  389.   char frame_attr=  attribute to use when drawing frame.
  390.   char win_attr= default attribute for window.
  391.  
  392.   Creates a window which is centered in the middle of the
  393.   computer screen.  It is called just like win_make(), except
  394.   that no row and column is specified.
  395.  
  396. *****************************************************************/
  397.  
  398. int win_center(int width,int height,char *array,char *title,
  399.                 char frame_attr, char win_attr)
  400. {
  401. extern struct screen_structure scr;
  402. extern struct window_structure w[];
  403.  
  404.  int x,y;
  405.  
  406.  x=(scr.columns/2)-(width/2); /* calculate window's column location*/
  407.  y=(scr.rows/2)-(height/2);    /* calculate window's row location */
  408.  
  409.  /* call win make to create centered window */
  410.  
  411.  return(win_make(x,y,width,height,array,title,frame_attr,win_attr));
  412.  
  413. }
  414.